
import type { Station } from "@/common/types/interface";
import { defineStore } from "pinia";
import privates from '@/static/privateRadios.json';
import { getRandomStations } from "@/common/station_api";
import { TIMEOUT } from "@/common/const";
import { shuffle } from 'lodash';
export const useStationStore = defineStore('stationStore', {
	state: () => ({
		stationList: [] as Station[],
		//精选电台
		featuredList: [] as Station[],
		//中文专区
		chineseList: [] as Station[],
		//随机电台
		randomList: [] as Station[],
		//私人定制
		privateList: privates as Station[]
	}),
	getters: {

	},
	actions: {
		async fetchFeaturedStations() {
			const res = await uni.request({
				url: 'https://de1.api.radio-browser.info/json/stations/topclick',
				method: 'GET',
				data: {
					hidebroken: true,
					order: 'clickcount',
					limit: 20,
					t: Date.now()
				},
				timeout: TIMEOUT
			});
			const stations = res.data as Station[];
			this.featuredList = shuffle(stations).slice(0, 5);
		},
		async fetchRandomStations() {
			const res = await getRandomStations();
			this.randomList = res as Station[];
		},
		async fetchChineseStations() {
			const res = await uni.request({
				url: 'https://de1.api.radio-browser.info/json/stations/bylanguage/chinese',
				method: 'GET',
				data: {
					hidebroken: true,
					order: 'random',
					limit: 6,
					t: Date.now()
				},
				timeout: TIMEOUT
			})
			this.chineseList = res.data as Station[];
		}
	},
	unistorage: true
});

